package gr.aueb.cs.mscis.chordroid;

import gr.aueb.cs.mscis.chordroid.app.FileApp;
import gr.aueb.cs.mscis.chordroid.chord.Node;
import gr.aueb.cs.mscis.chordroid.dht.DHTManager;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class ChordroidService extends Service {

	private static Node myLuckyNode;
	private static FileApp app;
	private static DHTManager dht;
	private static Thread t1, t2, t3;
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		setMyLuckyNode(new Node());
		t1 = new Thread(getMyLuckyNode());
		t1.start();

		setApp(new FileApp());
		t2 = new Thread(getApp());
		t2.start();
		
		setDht(new DHTManager());
		t3 = new Thread(dht);
		t3.start();

		return START_STICKY;
	}

	@Override
	public void onDestroy() {
		t1.interrupt();
		t2.interrupt();
		t3.interrupt();
		getMyLuckyNode().leave();
		Log.v("Makaritis", "just left");
		System.exit(0);
	}

	public static void setMyLuckyNode(Node myLuckyNode) {
		ChordroidService.myLuckyNode = myLuckyNode;
	}

	public static Node getMyLuckyNode() {
		return myLuckyNode;
	}

	public static FileApp getApp() {
		return app;
	}

	public static void setApp(FileApp app) {
		ChordroidService.app = app;
	}

	public static DHTManager getDht() {
		return dht;
	}

	public static void setDht(DHTManager dht) {
		ChordroidService.dht = dht;
	}

}